#include "XStormSubscriber.h"

namespace Xts
{
	namespace IceRuntime
	{
		XStormSubscriber::XStormSubscriber(const Ice::CommunicatorPtr& pIc, const Ice::LoggerPtr& pLogger, const Ice::ObjectAdapterPtr& pObjAdapter)
			:XStormBase(pIc, pLogger), x_pObjAdapter(pObjAdapter)
		{
		}

		XStormSubscriber::~XStormSubscriber(void)
		{
		}

		bool XStormSubscriber::SubscribeTopic(const ::std::string& topicName, const ::Ice::ObjectPtr& pSubServant,
				Ice::ObjectPrx& subscriberPrx)
		{
			/*if(!IsTopicManagerValid())
			{
				x_pLogger->error("Topic Manager is not initialized. Can't subscribe topic.");
				return false;
			}

			if(x_pIc->isShutdown() || x_pObjAdapter == NULL || x_pObjAdapter->isDeactivated())
			{
				x_pLogger->error("Ice runtime is not valid. Can't subscribe topic.");
				return false;
			}

			::IceStorm::TopicPrx topic = NULL;
			try
			{
				topic = x_topicManagerPrx->retrieve(topicName);
			}
			catch (const ::IceStorm::NoSuchTopic&)
			{
				try
				{
					topic = x_topicManagerPrx->create(topicName);
				}
				catch(const ::IceStorm::TopicExists&)
				{
					x_pLogger->error(topicName + ": temporary error. try again.");
					return false;
				}				
			}
			catch(const ::Ice::Exception& ex)
			{
				x_pLogger->error(ex.what());
				return false;
			}*/

			if(x_pIc->isShutdown() || x_pObjAdapter == NULL || x_pObjAdapter->isDeactivated())
			{
				x_pLogger->error("Ice runtime is not valid. Can't subscribe topic.");
				return false;
			}

			::IceStorm::TopicPrx topic = RetrieveTopic(topicName);
			if (topic == NULL)
			{
				x_pLogger->error(topicName + ": failed to get topic proxy.");
				return false;
			}			

			try
			{
				subscriberPrx = x_pObjAdapter->addWithUUID(pSubServant);
				topic->subscribeAndGetPublisher(x_qos, subscriberPrx);
				return true;
			}
			catch(const ::IceStorm::AlreadySubscribed)
			{
				x_pLogger->error(topicName + ": Already Subscribed.");
			}
			catch (const ::Ice::Exception& ex)
			{
				x_pLogger->error(ex.what());
			}
			return false;
		}

		bool XStormSubscriber::UnSubscribeTopic(const ::std::string& topicName, ::Ice::ObjectPrx& subscriberPrx)
		{
			/*if(!IsTopicManagerValid())
			{
				x_pLogger->error("Topic Manager is not initialized. Can't subscribe topic.");
				return false;
			}*/

			::IceStorm::TopicPrx topic = RetrieveTopic(topicName);
			if(topic == NULL)
			{
				x_pLogger->error(topicName + ": failed to get topic proxy.");
				return false;
			}

			try
			{
				//topic = x_topicManagerPrx->retrieve(topicName);
				topic->unsubscribe(subscriberPrx);
				x_pObjAdapter->remove(subscriberPrx->ice_getIdentity());
				return true;
			}
			catch (const ::IceStorm::NoSuchTopic&)
			{
				x_pLogger->error(topicName + " is not exist. Can't unsubscribe.");
			}
			catch (const ::Ice::Exception& ex)
			{
				x_pLogger->error(ex.what());
			}
			return false;
		}

		std::vector<std::string> XStormSubscriber::GetExistTopicNames()
		{
			::std::vector<::std::string> strTopics;
			if (IsTopicManagerValid())
			{
				try
				{
					::IceStorm::TopicDict topics = x_topicManagerPrx->retrieveAll();
					for (::IceStorm::TopicDict::iterator iter = topics.begin(); iter != topics.end(); iter++)
					{
						strTopics.push_back(iter->first);
					}
				}
				catch (const ::Ice::Exception& ex)
				{
					x_pLogger->error(ex.what());
				}
			}
			return strTopics;
		}
	}
}

